﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Cms.BLL.WepPart
{
    public class TabPage
    {

        public TabPage(string _tbid)
        {
            this.tbid = _tbid;
        }
        private string tbid = "";
        public string Tilte { get; set; }
        public Dictionary<string, string> Require = new Dictionary<string, string>();
        public string Builder()
        {
            string result = "";
            BLL.TabPage bll = new BLL.TabPage();
            Model.Sys_Tabpage TabPageModel = bll.GetById(tbid, false);
            if (TabPageModel != null)
            {
                result = TabPageModel.Layout;
                this.Tilte = TabPageModel.Title;
                if (TabPageModel.Tab_Type == Model.Sys_Tabpagetype.WptList)
                {
                    string content = TabPageModel.Layout;
                    if (!String.IsNullOrEmpty(content))
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        try
                        {
                            xmlDoc.LoadXml(content);
                            XmlNodeList Xnlwp = xmlDoc.SelectNodes("//div[@class=\"design-webpart\"]");//xmlDoc.SelectNodes("//w");
                            BLL.BaseDao<Model.Sys_Webpart, string> bllwpt = new BaseDao<Model.Sys_Webpart, string>();
                            foreach (XmlNode xn in Xnlwp)
                            {
                                string id = EntLib.Utiltiy.GetXmlAttribute(xn, "id");
                                string allowClose = EntLib.Utiltiy.GetXmlAttribute(xn, "allowclose");
                                string allowMax = EntLib.Utiltiy.GetXmlAttribute(xn, "allowmax");
                                string allowMin = EntLib.Utiltiy.GetXmlAttribute(xn, "allowmin");
                                string allowMove = EntLib.Utiltiy.GetXmlAttribute(xn, "allowmove");
                                string width = EntLib.Utiltiy.GetXmlAttribute(xn, "width");
                                string height = EntLib.Utiltiy.GetXmlAttribute(xn, "height");
                                Model.Sys_Webpart model = bllwpt.GetById(id, false);
                                string t = BuilderWpt(model,
                                    EntLib.TypeParse.StrToBool(allowClose, false),
                                    EntLib.TypeParse.StrToBool(allowMin, false),
                                    EntLib.TypeParse.StrToBool(allowMax, false),
                                    EntLib.TypeParse.StrToBool(allowMove, false), width, height);
                                result = result.Replace(xn.OuterXml, t);
                            }
                        }
                        catch(Exception e){
                            
                        }
                    }
                }
            }
            return result;
        }
        private string BuilderWpt(Model.Sys_Webpart wpt, bool allowclose, bool allowmin, bool allowmax, bool allowmove, string width, string height)
        {
            IWebpart Iwpt = null;
            string result = "";
            if (wpt.Webpart_Type == Model.WebPartType.Text)
            {
                Iwpt = new TextPart(wpt, allowclose, allowmin, allowmax, allowmove, width, height);
            }
            if (Iwpt != null)
                result = Iwpt.BuilderWpt();
            foreach (KeyValuePair<string, string> key in Iwpt.GetRequire())
            {
                if (!this.Require.ContainsKey(key.Key))
                    this.Require.Add(key.Key, key.Value);
                else {
                    string[] requires = key.Value.Split(',');
                    foreach (string r in requires) {
                        if (!this.Require[key.Key].Contains(r)) {
                            this.Require[key.Key] = this.Require[key.Key] + "," + r;
                        }
                    }
                }
            }
            return result;
        }
        public string BuilderHeader()
        {
            if (this.Require.Count < 1)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> key in this.Require)
            {
                switch (key.Key)
                {
                    case "style":
                    case "script":
                        string listStyle = key.Value;
                        if (!String.IsNullOrEmpty(listStyle))
                        {
                            string[] styles = listStyle.Split(',');
                            foreach (string style in styles)
                            {
                                switch (style)
                                {
                                    case "webpart":
                                        if (key.Key == "style")
                                            sb.Append("<link href=\"/App_Themes/default/webpart.css\" rel=\"stylesheet\" type=\"text/css\" />");
                                        if (key.Key == "script")
                                            sb.Append("<script src=\"/Scripts/webpart.js\" type=\"text/javascript\"></script>");
                                            break;
                                    case "rss":
                                        if (key.Key == "style")
                                            sb.Append("<link href=\"/App_Themes/default/rss.css\" rel=\"stylesheet\" type=\"text/css\" />");
                                        break;
                                    case "jquery":
                                        sb.Append("<script src=\"/Scripts/jquery-1.7.2.min.js\" type=\"text/javascript\"></script>");
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            return sb.ToString();
        }
    }
}
